import 'package:animated_interpolation/smart_tabbar.dart';
import 'package:animated_interpolation/sweet_indicator_painter.dart';
import 'package:find_friend/common/alert_tool.dart';
import 'package:find_friend/common/event_bus.dart';
import 'package:find_friend/common/play_audio_flr.dart';
import 'package:find_friend/const/const.dart';
import 'package:find_friend/pages/login&register/model/shared_user_info.dart';
import 'package:find_friend/pages/mine/mine_info.dart';
import 'package:find_friend/pages/mine/mine_xspace.dart';
import 'package:find_friend/pages/mine/view/mine_photowall_widget.dart';
import 'package:find_friend/pages/mine/view/mine_settinglist_widget.dart';
import 'package:find_friend/pages/mine/view/mine_sex_widget.dart';
import 'package:find_friend/pages/mine/view/mine_tag_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'mine_record.dart';


class MineHead extends StatefulWidget {
  
  final bool hiddenSignVoice;

  const MineHead({Key key, this.hiddenSignVoice = false}) : super(key: key);

  @override
  _MineHeadState createState() => _MineHeadState();
}

class _MineHeadState extends State<MineHead> with SingleTickerProviderStateMixin{

  TabController _tabController;
  PageController _pageController = PageController(initialPage: 0);
  SharedUserInfo userInfo = SharedUserInfo();
  int currTabIndex = 0;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(vsync: this, length: 2,);
    _initdata();
    EventMineBus.instance.addListener(EventMineKeys.UpdateMineInfo, (arg) { 
      _initdata();
    });
  }

  @override
  dispose(){
    EventMineBus.instance.removeListener(EventMineKeys.UpdateMineInfo);
    _tabController.dispose();
    _pageController.dispose();
    super.dispose();
  }

  _initdata() async {
    final user = await SharedUserInfo.instance();
    userInfo = user;
    if(mounted) setState(() { });
  }

  @override
  Widget build(BuildContext context) {

    final size = MediaQuery.of(context).size;
    final padding = MediaQuery.of(context).padding;
    return Container(
      height: size.height,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          GestureDetector(
            onTap: () {
              AlertTool.showBottomDialog(context, ["修改我的昵称"], onTapIndex: (index) async {
                Navigator.of(context).pop();
                Navigator.of(context).pushNamed("/minemodname");
              });
            },
            child: Container(
              width: size.width,
              padding: EdgeInsets.only(left: 95.5.w, top: 3),
              child: Text(userInfo.nickname ?? "昵称", 
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: instanceTextStyle(17, Colors.white))
              )
          ),
          GestureDetector(
            onTap: () async {
              bool isok = await push(context, MineUserInfoDetailPage());
              if(isok) _initdata();
            },
            child: Padding(
              padding: EdgeInsets.only(left: 95.5.w, top: 3),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  MineSexWidget(userInfo: userInfo,),
                  SizedBox(height: 5,),
                  MineTagWidget(label: userInfo.label,)
                ],
              ),
            ),
          ),

          // 语音签名
          Offstage(
            offstage: widget.hiddenSignVoice,
            child: Padding(
              padding: const EdgeInsets.only(left: 10.0),
              child: Row(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                  GestureDetector(
                    onLongPress: () async {
                      bool isok = await push(context, MineRecordPage());
                      if(isok) _initdata();
                    },
                    child: AudioFlrWidget(signvoice: userInfo.signvoice),
                  ),
                  SizedBox(width: 7),
                    InkWell(
                      onTap: () {
                        AlertTool.showBottomDialog(context, ["修改我的个性签名"], onTapIndex: (index) async {
                          Navigator.of(context).pop();
                          Navigator.of(context).pushNamed("/minemodsign");
                        });
                      },
                      child: Container(
                        width: size.width * 0.6,
                        height: 30.0,
                        alignment: Alignment.center,
                        child: Row(
                          children: [
                            Flexible(
                              flex: 1,
                              child: Text(
                                "${userInfo.bio ?? "这个人很懒，什么都没有留下"}", 
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(color: Colors.white, fontSize: 11,),
                              ),
                            ),
                            Image.asset("assets/bianji.png", height: 11, width: 11,color: Colors.white)
                          ],
                        )
                      ),
                    ),
                  ],
                ),
            ),
          ),

          Expanded(
            child: Container(
              width: size.width,
              margin: const EdgeInsets.only(top: 20),
              padding: EdgeInsets.only(bottom: 110 + padding.bottom * 0.25),
              color: Colors.white60,
              child: Column(
                children: [
                  PhotoWallWidget(list: userInfo.photowall ?? [],size: size,),
                  Container(
                    height: 30.h,
                    margin: EdgeInsets.symmetric(horizontal: 20),
                    child: SmartTabBar(
                      controller: _tabController,
                      labelColor: APP_main_color,
                      indicatorColor: APP_main_color,
                      indicatorBuilder: (context,controller,indicator,indicatorSize,tabKeys,old)=>SweetIndicatorPainter(
                        controller: controller,
                        indicatorSize: indicatorSize,
                        indicator: indicator,
                        tabKeys: tabKeys,
                        old: old
                      ),
                      onTap: (index){
                        setState(() {
                          currTabIndex = index;
                        });
                        _pageController.jumpToPage(index);
                      },
                      tabs: [
                        Text("我的设置", style: currTabIndex == 0 ? instanceTextStyle(17, APP_font_black, fontWeight: FontWeight.w500) : instanceTextStyle(15, const Color(0xff666666))),
                        Text("我的X区", style: currTabIndex == 1 ? instanceTextStyle(17, APP_font_black, fontWeight: FontWeight.w500) : instanceTextStyle(15, const Color(0xff666666))),
                        // Row(
                        //   mainAxisAlignment: MainAxisAlignment.end,
                        //   children: [
                        //     Text("情侣区", style: currTabIndex == 2 ? instanceTextStyle(17, APP_main_color, fontWeight: FontWeight.w600) : instanceTextStyle(15, const Color(0xff666666))),
                        //     Image.asset(currTabIndex == 2 ? "assets/qinglv-2.png" : "assets/qinglv-1.png")
                        //   ],
                        // )
                      ],
                    ),
                  ),
                  Expanded(
                    child: PageView(
                      controller: _pageController,
                      onPageChanged: (index){
                        setState(() {
                          currTabIndex = index;
                        });
                        _tabController.animateTo(index);
                      },
                      children: [
                        MineSettingListView(),
                        MineXSpaceView(mobile: userInfo.mobile,),
                        // MineXSpaceView(mobile: userInfo.mobile,)
                    ])
                  )
                ],
              ),
              // child: NestedScrollView(
              //   headerSliverBuilder: (context, bool) {
              //     return [
              //       SliverToBoxAdapter(
              //         child: PhotoWallWidget(list: userInfo.photowall ?? [],size: size,),
              //       ),
              //       SliverPersistentHeader(
              //         delegate: SliverTabBarDelegate(
              //           TabSelectorView(
              //             tabController: _tabController,
              //           ),
              //           // color: Colors.white,
              //         ),
              //         pinned: true,
              //       ),
              //     ];
              //   },
              //   body: TabBarView(
              //         controller: _tabController,
              //         children: [
              //         MineListView(),
              //         MineXSpaceView(mobile: userInfo.mobile,),
              //         MineXSpaceView(mobile: userInfo.mobile,)
              //       ],)
              // ),
            )
          )
        ],
      ),
    );
  }
}

// class SliverTabBarDelegate extends SliverPersistentHeaderDelegate {
//   final Widget widget;
//   final Color color;

//   const SliverTabBarDelegate(this.widget, {this.color})
//       : assert(widget != null);

//   @override
//   Widget build(
//       BuildContext context, double shrinkOffset, bool overlapsContent) {
//     return new Container(
//       child: widget,
//       color: color,
//     );
//   }

//   @override
//   bool shouldRebuild(SliverTabBarDelegate oldDelegate) {
//     return false;
//   }

//   @override
//   double get maxExtent => 30.h;

//   @override
//   double get minExtent => 30.h;
// }



